package platform.ui.charts;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import platform.ui.charts.ChartsTypeException;
import platform.ui.charts.TagUtils;
/**
 * 
 * 
 * @author   wg
 * @version Charts.java Apr 7, 2010  12:02:41 AM 
 *
 */
public class MyCharts extends TagSupport{

	private static final long serialVersionUID = -729411793807345909L;
	private static final String divPrefix = "chart_container_"; // 图表的容器div的id前缀
	private static final String chartPrefix = "chart_"; // 图表id的前缀
	
	private String type; //什么类型的图表
	private String xmlPath; // 请求xml文件的地址
	private String style; // 图表容器的样式默认为没有;
	private String width; // 图表的宽度
	private String height; // 图表的高度
	private String printBtn;  // 绑定打印到按钮
	private String container; // 容器的id
	private String swfPath; // 要用到的swf文件的地址 , 默认为"[basepath]fusionChartsFree/Charts/"
	
	
	private StringBuilder jsMsg = new StringBuilder();
	private StringBuilder jsPrint = new StringBuilder();
	
	
	@Override
	public int doEndTag() throws JspException {
		try {
			this.pageContext.getResponse().setCharacterEncoding("UTF-8");
			this.pageContext.getOut().write(jsMsg.toString()+"\n"+jsPrint.toString());
			System.out.println(jsMsg.toString()+"\n"+jsPrint.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		jsMsg = new StringBuilder();
		jsPrint = new StringBuilder();
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		// 判断并取出是什么类型的图表
		String swfName = ChartsConstantMap.getSwffileNameByType(this.getType());
		// 如果没有对应类型的就抛出异常
		if(null == swfName){
			throw new ChartsTypeException("没有对应于["+this.getType()+"]的图表类型");
		}
		// 生成图表的主程序
		jsMsg.append("<div id=\""+divPrefix+this.getContainer()+"\" style=\""+this.getStyle()+"\"></div>\n");
		jsMsg.append("<script type=\"text/javascript\">\n");
		jsMsg.append(chartPrefix+this.getId()+" = new FusionCharts (\""+this.getSwfPath()+swfName+"\",\""+this.getId()+"\",\""+this.getWidth()+"\",\""+this.getHeight()+"\");\n");
		jsMsg.append(chartPrefix+this.getId()+".setDataURL(escape(\""+this.getXmlPath()+"\"));\n");
		jsMsg.append(chartPrefix+this.getId()+".render(\""+divPrefix+this.getContainer()+"\");\n");
		jsMsg.append("</script>");
		// 如果用户要求打印
		if(null != this.getPrintBtn()){
			jsPrint.append("<script type=\"text/javascript\">\n");
			jsPrint.append("document.getElementById('"+this.getPrintBtn()+"').onclick = function(){\n");
			jsPrint.append("popUpWindow('"+this.getPrintPageURL(swfName)+"',0,0,"+(Integer.parseInt(this.getWidth())+150)+","+(Integer.parseInt(this.getHeight())+100)+");\n");
			jsPrint.append("};\n");
			jsPrint.append("var popUpWin=0;\n");
			jsPrint.append("function popUpWindow(URLStr, left, top, width, height)\n");
			jsPrint.append("{\n");
			jsPrint.append("  if(popUpWin)\n");
			jsPrint.append("  {\n");
			jsPrint.append("    if(!popUpWin.closed) popUpWin.close();\n");
			jsPrint.append("  }\n");
			jsPrint.append("  popUpWin = window.showModalDialog(encodeURI(URLStr),'printWin','dialogHeight:'+height+'px;dialogWidth:'+width+'px;center:yes;resizable:no;scroll:no');\n");
			jsPrint.append("}\n");
			jsPrint.append("</script>");
		}
		return SKIP_BODY;
	}

	@Override
	public void release() {
		jsMsg = null;
		super.release();
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getXmlPath() {
		if(xmlPath.indexOf("?")!=-1){
			return TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + xmlPath + "&wg"+UUID.randomUUID().toString().substring(0, 5)+"="+Math.random();
		}else{
			return TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + xmlPath + "?wg"+UUID.randomUUID().toString().substring(0, 5)+"="+Math.random();
		}
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public String getStyle() {
		if(null == this.style){
			this.style = "width:80%;height:80%;";
		}
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getWidth() {
		if(null == width){
			this.width = "400";
		}
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		if(null == height){
			this.height = "300";
		}
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getContainer() {
		if(null == container){
			this.container = UUID.randomUUID().toString().substring(0, 10).replaceAll("-", "");
		}
		return container;
	}

	public void setContainer(String container) {
		this.container = container;
	}

	public String getSwfPath() {
		if(null == swfPath){
			return this.swfPath = TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + "fusionChartsFree/Charts/";
		}
		return TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + swfPath;
	}

	public void setSwfPath(String swfPath) {
		this.swfPath = swfPath;
	}

	@Override
	public void setId(String id) {
		super.setId(id);
	}

	@Override
	public String getId() {
		if(null == id){
			this.id = UUID.randomUUID().toString().substring(0, 10).replaceAll("-", "");
		}
		return super.getId();
	}

	public String getPrintBtn() {
		return printBtn;
	}

	public void setPrintBtn(String printBtn) {
		this.printBtn = printBtn;
	}
	
	public String getFusionChartJSURL(){
		return TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + "fusionChartsFree/JSClass/FusionCharts.js";
	}
	
	public String getPrintPageURL(String swfName){
		String printPageURL = TagUtils.getBasePath((HttpServletRequest) this.pageContext.getRequest()) + "fusionChartsFree/chartPrint.jsp";
		printPageURL += "?style="+this.getStyle();
		printPageURL += "&swfPath="+this.getSwfPath()+swfName;
		printPageURL += "&width="+this.getWidth();
		printPageURL += "&height="+this.getHeight();
		printPageURL += "&xmlPath="+this.getXmlPath();
		return printPageURL;
	}
}
